Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
JavaScript function call inside another call allow you to access arguments that the "arguments[num]" can't access

i was learning in free code camp and i fell into a problem arguments[1] result into undefined when the function gets called like this functionName(arg1) (arg2) so for example arguments[1] would result in undefined but using this solved it return (second) => addTogether(first, second); and i don't know how a function call in the function itself could have accessed something that even the arguments[1] couldn't access, can anyone explain here is the full code if anyone is interested:

function addTogether() {
  const [first, second] = arguments;
  if (typeof(first) !== "number")
    return undefined;
  if (second === undefined)
    return (second) => addTogether(first, second);
  if (typeof(second) !== "number")
    return undefined;
  return first + second;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you call the function yourself with just one argument it returns another function.

The secret is that the value of first is already stored inside the closure that returns the other function from when you first called addTogether().

The returned function takes second as it's argument and it then calls addTogether() with the stored first from the initial call and the second just passed into the function calling addTogether again.

I added a console log inside addTogether to show the values of first and second for the two calls

function addTogether() {
  const [first, second] = arguments;
  console.log('first = ',first, ', second = ', second)
  if (typeof(first) !== "number")
    return undefined;
  if (second === undefined)
    // `first` is already defined above from the first call
    // `second` will be passed into this new function that gets returned when you call it later
    return (second) => addTogether(first, second);
  if (typeof(second) !== "number")
    return undefined;
  return first + second;
}

// store the returned function as a variable
const fn = addTogether(5);

// now call the returned function with `second` value as parameter
console.log('Total =',fn(10))//expect 15

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda